<?php
class Provincia extends Doctrine_Record {
    public function setTableDefinition() {
        $this->setTableName('provincia');
        $this->hasColumn('value', 'string', 150);
    }

    public function setUp() {
        $this->hasMany('Localidad as localidades', array(
            'local' => 'id',
            'foreign' => 'id_provincia',
            'onUpadate' => 'CASCADE'
        ));
    }
    
    //GETTERS Y SETTERS
    public function getValue () {
        return utf8_decode($this->_get('value'));
    }
    public function setValue ($q) {
        $this->_set('value', utf8_encode($q));
    }
    //FIN GETTERS Y SETTERS

    public static function toSelect ($objeto=false) {
        $html = '<select id="selectProvincia" name="provincia">';
        $provincias = Doctrine::getTable('provincia')->findAll();
        $html .= '<option selected="selected" value="">Elegir</option>';
        $idProv = ($objeto)?$objeto->localidad->provincia->id:0;
        foreach ($provincias as $prov) {
            $selected = ($prov->id == $idProv)?'  selected="selected"':'';
            $html .= '<option value="'.$prov->id.'"'.$selected.'>'.$prov->value.'</option>';
        }
        $html .= '</select>';
        return $html;
    }

}

?>